package com.stylefeng.guns.modular.channel.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.channel.service.*;
import com.stylefeng.guns.modular.system.model.Channel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.LoanOrder;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统计主渠道控制器
 *
 * @author fengshuonan
 * @Date 2018-08-27 19:56:49
 */
@Controller
@RequestMapping("/countChannelSuper")
public class CountChannelSuperController extends BaseController {

    private String PREFIX = "/channel/count/channelSuper/";

    @Autowired
    private IChannelSuperService channelSuperService;

    @Autowired
    private IChannelService channelService;

    @Autowired
    private IAppDeviceService deviceService;

    @Autowired
    private IAppUserService appUserService;

    @Autowired
    private ILoanOrderService loanOrderService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "channelSuper.html";
    }


    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Date beginDate, Date endDate) {

        List<Map<String, Object>> mapList = channelSuperService.selectMaps(null);

        int totalActiveCount = 0, totalRegisterCount = 0, totalRequestCount = 0, totalBargainCount = 0;
        BigDecimal totalBargainAmount = new BigDecimal(0.00);

        for (Map<String, Object> superChannelMap: mapList) {
            Long superChannelId = (Long) superChannelMap.get("id");
            /**
             * 查询子渠道
             */
            EntityWrapper<Channel> wrapper = new EntityWrapper<>();
            wrapper.where("super_channel_id={0}", superChannelId);
            List<Channel> channelList = channelService.selectList(wrapper);

            superChannelMap.put("subCount", channelList.size());
            int totalSubActiveCount = 0, totalSubRegisterCount = 0, totalSubRequestCount = 0, totalSubBargainCount = 0;
            BigDecimal totalSubBargainAmount = new BigDecimal(0.00);

            /**
             * 统计子渠道
             */
            for (Channel channel: channelList) {
                Long channelId = channel.getId();

                EntityWrapper channelWrapper = new EntityWrapper();
                channelWrapper.between(beginDate != null && endDate != null,"create_time", beginDate, endDate);//设置查询时间范围
                channelWrapper.where("channel_id={0}", channelId);
                int activeCount = deviceService.selectCount(channelWrapper);
                int registerCount = appUserService.selectCount(channelWrapper);

                int requestCount = loanOrderService.selectCountRequest(channelId, beginDate, endDate);
                int bargainCount = loanOrderService.selectCountBargain(channelId, beginDate, endDate);
                BigDecimal bargainAmount = loanOrderService.selectCountBargainAmount(channelId, beginDate, endDate);

                totalSubActiveCount = totalSubActiveCount + activeCount;
                totalSubRegisterCount = totalSubRegisterCount + registerCount;
                totalSubRequestCount = totalSubRequestCount + requestCount;
                totalSubBargainCount = totalSubBargainCount + bargainCount;
                totalSubBargainAmount =  totalSubBargainAmount.add(bargainAmount);
            }

            superChannelMap.put("activeCount", totalSubActiveCount);
            superChannelMap.put("registerCount", totalSubRegisterCount);
            superChannelMap.put("requestCount", totalSubRequestCount);
            superChannelMap.put("bargainCount", totalSubBargainCount);
            superChannelMap.put("bargainAmount", totalSubBargainAmount);
            superChannelMap.put("status", StatusEnum.desc((Integer) superChannelMap.get("isEnabled")));


            totalActiveCount = totalActiveCount + totalSubActiveCount;
            totalRegisterCount = totalRegisterCount + totalSubRegisterCount;
            totalRequestCount = totalRequestCount + totalSubRequestCount;
            totalBargainCount = totalBargainCount + totalSubBargainCount;
            totalBargainAmount = totalBargainAmount.add(totalSubBargainAmount);
        }
        HashMap<String, Object> countMap = new HashMap<>();
        countMap.put("activeCount", totalActiveCount);
        countMap.put("registerCount", totalRegisterCount);
        countMap.put("requestCount", totalRequestCount);
        countMap.put("bargainCount", totalBargainCount);
        countMap.put("bargainAmount", totalBargainAmount);
        mapList.add(0, countMap);
        return mapList;
    }



    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{loanOrderId}")
    @ResponseBody
    public Object detail(@PathVariable("loanOrderId") Integer loanOrderId) {
        return loanOrderService.selectById(loanOrderId);
    }
}
